<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class CheckCompanyAssessLogRule implements Rule
{
    protected $message;

    /**
     * Create a new rule instance.
     */
    public function __construct()
    {
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param string $attribute
     * @param mixed $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $mustColumns = [
            'company_id',
            'assess_admin_user_id',
            'assess_template_id',
            'assess_template_rule_id',
            'score',
        ];
        if (! is_array($value)) {
            $this->message = '数据格式异常';
        }
        foreach ($value as $ass) {
            foreach ($mustColumns as $mustColumn) {
                if (! isset($ass[$mustColumn])) {
                    $this->message = '缺少 ' . $mustColumn . ' 字段';
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return $this->message;
    }
}
